package opdr5b;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;


public class Console {
	private Connection connection;
	
	public Console() {
		try {
			connectionSetup();
			start();
		} catch (InstantiationException | SQLException | IllegalAccessException
				| ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Maak verbinding met de database
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	private void connectionSetup() throws InstantiationException, IllegalAccessException, SQLException, ClassNotFoundException {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection("jdbc:mysql://localhost/funda_site_2", "root", "");
	}
	
	
	private void start() throws SQLException {
		String sql = "SELECT MKMDWID, mkid FROM medewerkers";
		Statement stmt = connection.createStatement();
		stmt.execute(sql);
		ResultSet rs = stmt.getResultSet();
		while(rs.next()) {
			int mkid = rs.getInt(2);
			int mkmdwid = rs.getInt(1);
			insert(mkid, mkmdwid);
		}
		stmt.close();
	}
	
	
	private void insert(int mkid, int mkmdwid) throws SQLException {
		
		String sql = "INSERT INTO mkantoor_medewerkers (mkid, mkmdwid) VALUES ("+mkid + ", "+mkmdwid + ")";
		Statement stmt = connection.createStatement();
		stmt.executeUpdate(sql);
		stmt.close();
		//System.out.println(sql);
	}

}
